package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.sc.nft.dao.ExportRecordsDao;
import com.sc.nft.dao.TempCityServerCentreDao;
import com.sc.nft.entity.ExportRecords;
import com.sc.nft.entity.TempCityServerCentre;
import com.sc.nft.entity.vo.CityServerCentreExportVO;
import com.sc.nft.entity.vo.WorkRoomExportVO;
import com.sc.nft.enums.ApplicationRequirementUpOrDownEnum;
import com.sc.nft.enums.ExportRecordsTypeEnum;
import com.sc.nft.enums.ExportTypeEnum;
import com.sc.nft.enums.ExportTypeStatusEnum;
import com.sc.nft.mapper.TempCityServerCentreMapper;
import com.sc.nft.pool.SysMemberPool;
import com.sc.nft.service.TempCityServerCentreService;
import com.sc.nft.service.UserCityServerCentreService;
import com.sc.nft.service.UserWorkRoomService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 朱梦晨
 * @Description: 城市服务中心/社区网点
 * @date 2024-07-25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TempCityServerCentreServiceImpl implements TempCityServerCentreService {


    private final ExportRecordsDao exportRecordsDao;

    private final UserCityServerCentreService userCityServerCentreService;

    private final TempCityServerCentreMapper tempCityServerCentreMapper;

    private final UserWorkRoomService userWorkRoomService;

    private final TempCityServerCentreDao tempCityServerCentreDao;

    /**
     * 增加城市服务中心/社区网点导出数据 -> 数据库
     */
    @Override
    public Boolean saveTempCityServerCentreOrWorkRoom(Integer type) {
        Long exportRecordsId = getExportRecordsId(type);
        try {
            //城市中心
            if (ApplicationRequirementUpOrDownEnum.CITY.getCode().equals(type)) {
                List<CityServerCentreExportVO> cityServerCentreExportVOList = userCityServerCentreService.centerServerExport();
                List<TempCityServerCentre> tempCityServerCentreList = BeanUtil.copyToList(cityServerCentreExportVOList, TempCityServerCentre.class);
                tempCityServerCentreList.forEach(t -> {
                    t.setExportRecordsId(exportRecordsId);
                    t.setType(ApplicationRequirementUpOrDownEnum.CITY.getCode());
                });
                tempCityServerCentreMapper.saveBatch(tempCityServerCentreList);
            } else {
                //社区网点
                List<WorkRoomExportVO> workRoomExportVOList = userWorkRoomService.workRoomExport();
                List<TempCityServerCentre> tempCityServerCentreList = BeanUtil.copyToList(workRoomExportVOList, TempCityServerCentre.class);
                tempCityServerCentreList.forEach(t -> {
                    t.setExportRecordsId(exportRecordsId);
                    t.setType(ApplicationRequirementUpOrDownEnum.USER_WORK_ROOM.getCode());
                });
                tempCityServerCentreMapper.saveBatch(tempCityServerCentreList);
            }
        } catch (Exception e) {
            //导出失败
            log.error("城市服务中心 or 社区网点列表导出失败:", e);

            return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_FAIL.getCode());
        }

        return exportRecordsDao.updateStatusById(exportRecordsId, ExportTypeStatusEnum.EXPORT_STATUS_SUCCESS.getCode());
    }

    private static Long getExportRecordsId(Integer type) {
        StringBuilder exportName = new StringBuilder();
        int exportType = 0;
        if (ApplicationRequirementUpOrDownEnum.CITY.getCode().equals(type)) {
            exportName.append(ExportTypeEnum.TEMP_CITY_SERVER_CENTRE.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_CITY_SERVER_CENTRE.getCode();
        } else {
            exportName.append(ExportTypeEnum.TEMP_COMMUNITY_NETWORK.getName()).append(DateUtil.format(DateUtil.date(), "yyyy年MM月dd日")).append(".xlsx");
            exportType = ExportTypeEnum.TEMP_COMMUNITY_NETWORK.getCode();
        }
        //增加导出记录
        ExportRecords exportRecords = new ExportRecords();
        exportRecords.setExportName(exportName.toString());
        exportRecords.setExportType(exportType);
        exportRecords.setStatus(ExportTypeStatusEnum.EXPORT_STATUS_IN.getCode());
        exportRecords.setCreatorId(SysMemberPool.getMember().getId());
        exportRecords.setType(ExportRecordsTypeEnum.ADMIN);
        exportRecords.insert();
        Long exportRecordsId = exportRecords.getId();
        return exportRecordsId;
    }


    /**
     * 下载  根据导出记录Id
     *
     * @param exportRecordsId
     * @return
     */
    @Override
    public List<TempCityServerCentre> findTempCityServerCentreList(Long exportRecordsId) {
        return tempCityServerCentreDao.getTempCityServerCentreList(exportRecordsId);
    }

}
